package ddbs;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import org.json.JSONArray;
import org.json.JSONException;

public class Data {
    String filednames[] = null;
    JSONArray jarray = new JSONArray();

    public void showFiledNmaes() {
        if (filednames == null) {
            System.out.println("filednames null");
        } else {
            for (int i = 0; i< this.filednames.length; ++i){
                System.out.println("filedname["+i+"]="+filednames[i]);
            }
        }
    }
    public Data() {
    }
    public Data(JSONArray jarray) {
        JSONArray fileds = (JSONArray) jarray.remove(jarray.length() - 1);
        filednames = new String[fileds.length()];
        for (int j = 0; j < fileds.length(); ++j) {
            try {
                filednames[j] = fileds.getString(j);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        this.jarray = jarray;
        //System.out.println("Recieve Data size = "+this.jarray.length());
    }

    public Data(ResultSet rs) {
        Object fileds[] = null;
        ResultSetMetaData meta = null;
        try {
            meta = rs.getMetaData();
            int columns = meta.getColumnCount();
            // System.out.println("columns = " + columns);
            filednames = new String[columns];
            for (int i = 0; i < columns; ++i) {
                filednames[i] = meta.getColumnName(i + 1);
                // System.out.print(filednames[i] + "\t");
            }
            fileds = new Object[columns];
            while (rs.next()) {
                for (int i = 0; i < columns; ++i) {
                    Object obj = rs.getObject(i + 1);
                    fileds[i] = obj;
                    // System.out.print(obj + "\t");
                }
                jarray.put(new JSONArray(fileds));
                // System.out.println();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        // System.out.println(this.serialize());
        //System.out.println("Data size = "+this.jarray.length());
    }

    public char[] serialize() {
        try {
            this.jarray.put(new JSONArray(filednames));

        } catch (JSONException e) {
            e.printStackTrace();
        }

        String s = this.jarray.toString();
        this.jarray.remove(this.jarray.length() - 1);
        return s.toCharArray();
    }

    public static Data deserialize(int len, String content) {
        try {
            return new Data(new JSONArray(content));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    public StringBuffer generateHtmlTable() {
        StringBuffer sb = new StringBuffer();
        int length = jarray.length();
        sb.append("<tr>");
        for (int j = 0; j < filednames.length; ++j) {
            sb.append("<th>" + filednames[j] + "</th>");
        }
        sb.append("</tr>\n");
        for (int i = 0; i < length; ++i) {
            try {
                sb.append("<tr>");
                JSONArray row = (JSONArray) jarray.get(i);
                for (int j = 0; j < row.length(); ++j) {
                    sb.append("<th>" + row.getString(j) + "</th>");
                }
                sb.append("</tr>\n");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        // System.out.println(sb.toString());
        return sb;
    }

    Data merge(Data ds) {
        if (ds == null)
            return this;
        for (int i = 0; i < ds.jarray.length(); ++i) {
            try {
                JSONArray row = (JSONArray) ds.jarray.get(i);
                this.jarray.put(row);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return this;
    }
    int getSize() {
        if(jarray == null)
            return 0;
        return jarray.length();
    }
}
